/* eslint-disable array-callback-return */
import { message } from 'antd';
import { getAddEmpDetail, getAddEmpDetailed } from '@/services/addass';

export default {
  state: {
    sendId: null,
    responseAddAssBaseDto: {}, // 基本信息
    responseAddAssSendDto: {}, // 派单信息
    responseDeduAssDto: {}, // 已经减少的险种
    chargeDto: {}, // 收费信息
    rsponseDeduPdDto: {}, // 产品信息
    handDto: [], // 	办理备注
    proRecord: [], // 问题记录
    dataDtos: [], // 材料信息
    recordList: [], // 联系记录
    responseAddAssDto: {}, // 新增险种信息
    changeQuoDto: {}, // 新增产品信息（报价单）
    serDto: {}, // 社保公积金信息
    dataList: {}, // 总数据
    execStatusDto: {}, // 执行状态
    dataDetailed: [], // 社保公积金缴纳明细
    coltFlag: 1, // 是否需要材料信息
    laborContDto: {
      responseLaborCont: {
        serType: ''
      }
    },
  },
  effects: {
    // 详情数据
    *fetchAddEmpDetail({ payload }, { call, put }) {
      const response = yield call(getAddEmpDetail, payload);
      if (response.success) {
        if (
          response.data.rsponseDeduPdDto &&
          response.data.rsponseDeduPdDto.pdDetailDtos &&
          response.data.rsponseDeduPdDto.pdDetailDtos.length
        ) {
          response.data.rsponseDeduPdDto.pdDetailDtos.map((item, index) => {
            const obj = item;
            if (item.deduTaskDets && item.deduTaskDets.length) {
              item.deduTaskDets.map((ass, i) => {
                const temp = ass;
                temp.pdName = ass.assName;
                temp.id = `${index}-${i}`;
              });
            }
            obj.id = `${index}`;
            if (item.deduTaskDets) {
              obj.children = item.deduTaskDets || [];
            }
          });
        }

        const sortNumber = (a, b) => a.operLeafType - b.operLeafType;
        if (response.data.execStatusDto && response.data.execStatusDto.leafList) {
          response.data.execStatusDto.leafList = response.data.execStatusDto.leafList.sort(
            sortNumber
          );
        }
        yield put({
          type: 'updateModelDatas',
          payload: {
            ...payload,
            ...response.data,
          },
        });
      } else {
        message.warning(response.message || '获取人员信息：服务端错误');
      }
    },
    *fetchAddEmpDetailed({ payload }, { call, put }) {
      const response = yield call(getAddEmpDetailed, payload);
      yield put({
        type: 'dateModelAddEmpDetailed',
        payload: [],
      });
      if (response.success) {
        yield put({
          type: 'dateModelAddEmpDetailed',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '查询失败');
      }
    },
  },

  reducers: {
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
        // responseAddAssSendDto: payload.sendDto,
        // responseAddAssBaseDto: payload.basicDto,
      };
    },

    updateModelDatas(state, { payload }) {
      return {
        ...state,
        // ...payload,
        dataList: payload,
        responseAddAssSendDto: payload.responseAddAssSendDto,
        responseAddAssBaseDto: payload.responseAddAssBaseDto,
        laborContDto: payload.laborContDto,
        responseAddAssDto: payload.responseAddAssDto || {},
        chargeDto: payload.chargeDto || {},
        serDto: payload.serDto || {},
        execStatusDto: payload.execStatusDto || null,
        handDto: payload.handDto || [],
        proRecord: payload.probDets || [],
        dataDtos: payload.dataDtos || [],
        coltFlag: payload.coltFlag || 1,
        changeQuoDto: payload.changeQuoDto || {},
      };
    },
    dateModelAddEmpDetailed(state, { payload }) {
      const list = payload && Array.isArray(payload) ? payload : [];
      return {
        ...state,
        dataDetailed: list,
      };
    },
  },
};
